package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.mapper.ConsumptionItemMapper;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionItemServletImpl implements IConsumptionItemService {
    @Autowired
    private ConsumptionItemMapper consumptionItemMapper;
    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Override
    public void delete(Long id) {
        consumptionItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(ConsumptionItem consumptionItem) {
        consumptionItem.setCreateTime(new Date());
        consumptionItem.setCreateUserId(UserContext.getEmployee().getId());
        consumptionItemMapper.insert(consumptionItem);
        Consumption consumption = consumptionMapper.selectByCno(consumptionItem.getCno());
        consumption.setTotalAmount(consumption.getTotalAmount().add(consumptionItem.getAmount()));
        consumption.setDiscountAmount(consumption.getDiscountAmount().add(consumptionItem.getDiscountAmount()));
        consumption.setPayAmount(consumption.getPayAmount().add(consumptionItem.getPayAmount()));

        consumptionMapper.updateSum(consumption);


    }

    @Override
    public ConsumptionItem get(Long id) {
        return  consumptionItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ConsumptionItem> listAll() {
        return consumptionItemMapper.selectAll();
    }

    @Override
    public void update(ConsumptionItem consumptionItem) {
        consumptionItemMapper.updateByPrimaryKey(consumptionItem);

    }

    @Override
    public PageInfo<ConsumptionItem> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
           List<ConsumptionItem> consumptionItems = consumptionItemMapper.selectForList(qo);
        return new PageInfo(consumptionItems);
    }

    @Override
    public List<ConsumptionItem> queryByCno(String cno) {

        return consumptionItemMapper.selectByCno(cno);
    }
}
